package br.com.pontoe.model.persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.pontoe.model.conexao.Conexao;
import br.com.pontoe.model.entidades.Cargo;
import br.com.pontoe.model.entidades.TipoPermissao;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;


/**
 * DAO para a entidade Cargo
 * 
 * @author Rodolfo
 * @version 1.0
 * @since 08/05/2010
 */

public class CargoDAO implements DAOInterface<Cargo> {
	private StringBuilder sql = new StringBuilder();
	private Connection con = null;
	private PreparedStatement stm = null;

	@Override
	public int adicionar(Cargo entidade) throws SQLException {
		int ret = -1;

		try {
			sql = new StringBuilder();
			sql.append("INSERT INTO CARGO (IDCARGO, NOME, DESCRICAO, TPPERMISSAO) ");
			sql.append("VALUES (NULL, \'?\', \'?\', ?) ");

			con = Conexao.getInstance().getConnection();
			stm = (PreparedStatement) con.prepareStatement(sql.toString());

			stm.setString(1, entidade.getNome());
			stm.setString(2, entidade.getDescricao());
			stm.setInt(3, entidade.getTpPermissao().ordinal());

			ret = stm.executeUpdate();
		} 
		finally {
			stm.close();
			con.close();
		}
		return ret;
	}

	@Override
	public boolean atualizar(Cargo entidade) throws SQLException {
		boolean ret = true;

		try {
			sql = new StringBuilder();
			sql.append("UPDADE CARGO ");
			sql.append("SET   NOME = \'?\', DESCRICAO = \'?\', TPPERMISSAO = ? ");
			sql.append("WHERE IDCARGO = ? ");

			con = Conexao.getInstance().getConnection();
			stm = (PreparedStatement) con.prepareStatement(sql.toString());

			stm.setString(1, entidade.getNome());
			stm.setString(2, entidade.getDescricao());
			stm.setInt(3, entidade.getTpPermissao().ordinal());

			ret = stm.execute();
		} 
		finally {
			stm.close();
			con.close();
		}
		return ret;
	}

	@Override
	public Cargo localizar(int id) throws SQLException {
		Cargo ret = null;

		try {
			sql = new StringBuilder();
			sql.append("SELECT 	IDCARGO, NOME, DESCRICAO, TPPERMISSAO ");
			sql.append("FROM 	CARGO ");
			sql.append("WHERE 	IDCARGO = ? ");

			con = Conexao.getInstance().getConnection();
			stm = (PreparedStatement) con.prepareStatement(sql.toString());
			
			stm.setInt(1, id);

			ResultSet temp = stm.executeQuery();

			if (temp.next()) {
				ret = new Cargo();
				ret.setId(temp.getInt("IDCARGO"));
				ret.setNome(temp.getString("NOME"));
				ret.setDescricao(temp.getString("DESCRICAO"));
				ret.setTpPermissao(TipoPermissao.values()[temp.getInt("TPPERMISSAO")]);
			}
			temp.close();
		} 
		finally {
			stm.close();
			con.close();
		}
		return ret;
	}

	@Override
	public List<Cargo> localizarTodos() throws SQLException {
		List<Cargo> ret = new ArrayList<Cargo>();

		try {
			sql = new StringBuilder();
			sql.append("SELECT 	IDCARGO, NOME, DESCRICAO, TPPERMISSAO ");
			sql.append("FROM 	CARGO ");

			con = Conexao.getInstance().getConnection();
			stm = (PreparedStatement) con.prepareStatement(sql.toString());

			ResultSet temp = stm.executeQuery();

			while (temp.next()) {
				Cargo c = new Cargo();
				c.setId(temp.getInt("IDCARGO"));
				c.setNome(temp.getString("NOME"));
				c.setDescricao(temp.getString("DESCRICAO"));
				c.setTpPermissao(TipoPermissao.values()[temp.getInt("TPPERMISSAO")]);
				ret.add(c);
			}
			temp.close();
		} 
		finally {
			stm.close();
			con.close();
		}
		return ret;
	}

	@Override
	public boolean remover(Cargo entidade) throws SQLException {
		boolean ret = true;

		try {
			sql = new StringBuilder();
			sql.append("DELETE FROM CARGO");
			sql.append("WHERE  IDCARGO = ? ");

			con = Conexao.getInstance().getConnection();
			stm = (PreparedStatement) con.prepareStatement(sql.toString());
			
			stm.setInt(1, entidade.getId());
			
			ret = stm.execute();
		} 
		finally {
			stm.close();
			con.close();
		}
		return ret;
	}

}
